package com.yadong.numberguess;

import com.yadong.numberguess.Game.GameType;

import android.content.Context;
import android.content.SharedPreferences;

public class DataSaver {
    private static final String PREFERENCE_NAME = "numberguess";
    private static final String GAME_HARD_LESS_STEP = "hard_less_step";
    private static final String GAME_HARD_LESS_TIME = "hard_less_time";
    private static final String GAME_SIMPLE_LESS_STEP = "simple_less_step";
    private static final String GAME_SIMPLE_LESS_TIME = "simple_less_time";
    private static final String GAME_MODE = "game_mode";

    private Context mContext;
    private SharedPreferences mPreferences;

    public DataSaver(Context context) {
        this.mContext = context;
        mPreferences = mContext.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
    }

    public long getLessTime(GameType gameType) {
        if (gameType == GameType.SIMPLE) {
            return mPreferences.getLong(GAME_SIMPLE_LESS_TIME, -1);
        } else {
            return mPreferences.getLong(GAME_HARD_LESS_TIME, -1);
        }
    }

    public void setLessTime(GameType gameType, long time) {
        if (gameType == GameType.SIMPLE) {
            mPreferences.edit().putLong(GAME_SIMPLE_LESS_TIME, time).apply();
        } else {
            mPreferences.edit().putLong(GAME_HARD_LESS_TIME, time).apply();
        }
    }

    public int getLessStep(GameType gameType) {
        if (gameType == GameType.SIMPLE) {
            return mPreferences.getInt(GAME_SIMPLE_LESS_STEP, -1);
        } else {
            return mPreferences.getInt(GAME_HARD_LESS_STEP, -1);
        }
    }

    public void setLessStep(GameType gameType, int step) {
        if (gameType == GameType.SIMPLE) {
            mPreferences.edit().putInt(GAME_SIMPLE_LESS_STEP, step).apply();
        } else {
            mPreferences.edit().putInt(GAME_HARD_LESS_STEP, step).apply();
        }
    }

    public int getGameMode() {
        return mPreferences.getInt(GAME_MODE, 0);
    }

    public void setGameMode(int mode) {
        mPreferences.edit().putInt(GAME_MODE, mode).apply();
    }
}
